﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tanis.RouteService;
using Tanis.Settings;

namespace Tanis.ViewModels
{
    public class MainViewModel //: INotifyPropertyChanged
    {
        #region Contructor
        public MainViewModel()
        {
            Routes = new ObservableCollection<Route>();
			Cities = new ObservableCollection<ScopeInfo>();

            App.ServiceManager.CityListLoaded += ServiceManager_CityListLoaded;
            App.ServiceManager.StopListLoaded += ServiceManager_StopListLoaded;

        	CurrentRoute = null;
        }
        #endregion

        #region Private members and properties
        private volatile bool _stopListLoaded;
        private volatile bool _cityListLoaded;
        #endregion

        #region Public properties
        public ObservableCollection<Route> Routes { get; private set; }
        public ObservableCollection<ScopeInfo> Cities { get; private set; }
        public Route CurrentRoute { get; set; }
        #endregion

        #region Event declarations
        public event EventHandler SearchPerfomed;
        public event EventHandler SearchPerfoming;
        public event EventHandler<DetailsRequestedArgs> DetailsRequested;
        public event EventHandler DataLoaded;
        public event EventHandler CityListLoaded;
        public event EventHandler StopListLoaded;
        #endregion

        #region Data loading (public)
        public  bool IsDataLoaded
        {
            get
            {
                if (_cityListLoaded && _stopListLoaded)
                {
                    return true;
                }
                return false;
            }
            private set
            {
                _cityListLoaded = value;
                _stopListLoaded = value;
            }
        }

        public void LoadData()
        {
            LoadCityList();
            LoadStopList();
        }
        public void LoadCityList()
        {
            _cityListLoaded = false;
            App.ServiceManager.GetCityList();
        }
        public void LoadStopList()
        {
            _stopListLoaded = false;
            App.ServiceManager.GetStopList(SettingsControl.DefaultCity);
        }
        public void OnDetailsRequested(DetailsRequestedArgs e)
        {
            EventHandler<DetailsRequestedArgs> handler = DetailsRequested;
            if (handler != null) handler(this, e);
        }
        public void OnSearchPerfomed()
        {
            EventHandler handler = SearchPerfomed;
            if (handler != null) handler(this, null);
        }

        public void OnSearchPerfoming()
        {
            EventHandler handler = SearchPerfoming;
            if (handler != null) handler(this, null);
        }
        #endregion

        #region Data loading (private)
        private void ServiceManager_StopListLoaded(object sender, GetStopNamesCompletedEventArgs e)
        {
            _stopListLoaded = true;
            if (IsDataLoaded)
            {
                DataLoaded(sender, null);
            }
            if (StopListLoaded != null)
            {
                StopListLoaded(sender, null);
            }
        }

        private void ServiceManager_CityListLoaded(object sender, GetAvailableMobileScopesCompletedEventArgs e)
        {
            Cities.Clear();

            if (e.Result == null)
            {
                return;
            }

            foreach (var city in e.Result)
            {
                if (city.Visible)
                {
                    App.ViewModel.Cities.Add(city);
                }
            }
            _cityListLoaded = true;
            if (IsDataLoaded)
            {
                DataLoaded(sender, null);
            }
            if (CityListLoaded != null)
            {
                CityListLoaded(sender, null);
            }
        }
        #endregion
    }
}